/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.cspimp.psp.service.impl;

import me.zhengjie.modules.cspimp.psp.domain.WorkPermit;
import me.zhengjie.modules.cspimp.psp.domain.vo.WorkPermitVo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.psp.repository.WorkPermitRepository;
import me.zhengjie.modules.cspimp.psp.service.WorkPermitService;
import me.zhengjie.modules.cspimp.psp.service.dto.WorkPermitDto;
import me.zhengjie.modules.cspimp.psp.service.dto.WorkPermitQueryCriteria;
import me.zhengjie.modules.cspimp.psp.service.mapstruct.WorkPermitMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zmm
 * @website https://docs.auauz.net
 * @description 服务实现
 * @date 2020-05-24
 **/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WorkPermitServiceImpl implements WorkPermitService {

    private final WorkPermitRepository workPermitRepository;
    private final WorkPermitMapper workPermitMapper;

    @Override
    public Map<String, Object> queryAll(WorkPermitQueryCriteria criteria, Pageable pageable) {
        Page<WorkPermit> page = workPermitRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(workPermitMapper::toDto));
    }

    @Override
    public List<WorkPermitDto> queryAll(WorkPermitQueryCriteria criteria) {
        return workPermitMapper.toDto(workPermitRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public WorkPermitDto findById(Long id) {
        WorkPermit workPermit = workPermitRepository.findById(id).orElseGet(WorkPermit::new);
        ValidationUtil.isNull(workPermit.getId(), "WorkPermit", "id", id);
        return workPermitMapper.toDto(workPermit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkPermitDto create(WorkPermitVo resources) {
        WorkPermit workPermit = new WorkPermit();
        BeanUtils.copyProperties(resources, workPermit);
        workPermit.setCid(0l);
        return workPermitMapper.toDto(workPermitRepository.save(workPermit));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WorkPermitVo resources) {
        WorkPermit workPermit = workPermitRepository.findById(resources.getId()).orElseGet(WorkPermit::new);
        ValidationUtil.isNull(workPermit.getId(), "WorkPermit", "id", resources.getId());
        BeanUtils.copyProperties(resources, workPermit);
        workPermitRepository.save(workPermit);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            workPermitRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<WorkPermitDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WorkPermitDto workPermit : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("类型", workPermit.getType());
            map.put("状态", workPermit.getStatus());
            map.put("申请人", workPermit.getApplicant());
            map.put("申请单位", workPermit.getApplicantUnit());
            map.put("作业级别", workPermit.getWorkLevel());
            map.put("作业方式", workPermit.getWorkMode());
            map.put("作业地点", workPermit.getWorkLocation());
            map.put("作业时间", workPermit.getWorkDate());
            map.put("作业负责人", workPermit.getWorkDutyName());
            map.put("作业实施人", workPermit.getWorkImplementer());
            map.put("作业备注", workPermit.getWorkRemark());
            map.put("创建时间", workPermit.getCreateTime());
            map.put("更新时间", workPermit.getUpdateTime());
            map.put("创建者", workPermit.getCreateBy());
            map.put("更新者", workPermit.getUpdateBy());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}